<?php
class ShopController extends AppController {

	public $name = 'shop';
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','lockshop','goodsdownsale','getshopcategories','getareas','shopedit','dorequest','request','requestview','categorieslist','categoriesadd','categoriesedit','categoriesdel');

	}
	
	public function index() {	
		$query = array(
			'order' => ' sort_order asc,id asc ', 
			'fields' => array('shopcategories.*'), 
		);
		$result_category = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);		
		$category_dg = $this->get_dg_category($result_category);
		$query = array(
			'order' => ' id asc ', 
			'fields' => array('userrank.*'), 
		);
		$user_rank = POP::exe('[@@:UM userrank][@@:QU find:all]',$query);		
		$userrank = array();
	    foreach($user_rank as $value) {
		    $userrank[$value['userrank']['id']] = $value['userrank'];
	    }
        //获得地区列表		
		$query = array(
			'fields' => array('area.*'), 
		);
		$array = POP::exe('[@@:UM area][@@:QU find:all]',$query);		

	    $areainfo = array();
	    foreach($array as $k=>$v) {
		    $areainfo[$v['area']['id']] = $v['area']['area_name'];
	    }	
        $page = Request::get_args('page');		
        $name = Request::get_args('name');
        $start_time = Request::get_args('start_time');
        $end_time = Request::get_args('end_time');
        $shop_open_status = Request::get_args('shop_open_status');
        $shop_categorie = Request::get_args('shop_categorie');
        $conditions = array();
        if($name) {
		    $conditions['shopinfo.shop_name like'] = "%$name%";
        }
        if($start_time) {
		    $conditions['shopinfo.created >='] = "$start_time";		
        }
        if($end_time) {
		    $conditions['shopinfo.created <='] = "$end_time";		
        }
        if($shop_open_status!="" && $shop_open_status!=2) {
		    $conditions['shopinfo.lock_flg'] = $shop_open_status;
        }

        if($shop_categorie) {
	        $categories_str = array();
	        $parent_id = '';
	        foreach($category_dg as $v) {
		        if($v['id'] == $shop_categorie) {
			        if($v['parent_id']=='0'){
				        $parent_id = $v['id'];
			        } else {
				        $categories_str[$shop_categorie] = $shop_categorie;
			        }
			        break;
		        }
	        }
	        if($parent_id){
		        foreach($category_dg as $v) {
			        if($v['parent_id'] == $parent_id){
				        $categories_str[$v['id']] =$v['id'];
			        }
		        }
	        }
		    $conditions['shopinfo.shop_categories'] = $categories_str;			
        }
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'shopinfo',
 		            'joins' => array(
					    array('type'=>'left','table'=>'user','conditions'=>array('shopinfo.user_id' =>'user.id')),					
						),
				    'fields' => array('shopinfo.*','user.*'),
					'conditions' => $conditions,
					'order' => ' shopinfo.created desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 
        $this->set('result',$result);
        $this->set('areainfo',$areainfo);		
        $this->set('name',$name);		
        $this->set('userrank',$userrank);		
        $this->set('category_dg',$category_dg);
		
		
	}
	
	public function dorequest() {
        /* post 数据处理 */
        $request_id = intval(Request::get_args('id'));
        $s = intval(Request::get_args('s'));
        $requestid=Request::get_args('request_id');

        if($requestid){
	        //$request_id=implode(",", $requestid);
        }else{
	        $request_id = intval(Request::get_args('id'));
        }
        if(!$request_id) {
		    $this->flash("参数错误！", Request::referer());
		}
		$query = array(
			'conditions' => array('shopinfo.id' => $request_id), 
			'fields' => array('shopinfo.user_id'), 
		);
		$rs = POP::exe('[@@:UM shopinfo][@@:QU find:all]',$query);	
		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['shoprequest'] = array('status'=>$s);;				
		$query['set'] = $safeData;	
		$query['conditions'] = array('shoprequest.id'=>$request_id);		
		POP::exe('[@@:UM shoprequest][@@:QU save]',$query);		
		$user_id='';
	    if($s=='1') {
		    $userid_array = array();
		    foreach($rs as $key=>$val){
			    $userid_array[$key]=$val['shopinfo']['user_id'];
		    }
			$query  = array();		
			$query['keyid'] = true;
			$safeData['user'] = array('rank_id'=>2);;				
			$query['set'] = $safeData;	
			$query['conditions'] = array('user.id'=>$userid_array);		
			POP::exe('[@@:UM user][@@:QU save]',$query);			
	    }

		$array = array(
			'log_time' => date("Y-m-d H:i:s"),
			'admin_id' => $this->my->id,
			'admin_name' => $this->my->username,
			'log_info' => "审核操作"."：$user_id",
			'ip_address' => Request::getclientIp(),					
		);
		$safeData['adminlog'] = $array;									
		POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);			
        $this->flash("操作成功！", Request::referer()); 	
	
    }	
	
	public function request() {		
        $page = Request::get_args('page');	
        $s = intval(Request::get_args('s'));
        $conditions = array();		
        if(!$s){
	        $s='100';
        }		
		if($s&&$s!='10') {
	        $status = $s;
	        if($s=='100') { $status=0; }
			$conditions['shoprequest.status'] = $status;
        }
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'shoprequest',
 		            'joins' => array(
					    array('type'=>'left','table'=>'shopinfo','conditions'=>array('shoprequest.user_id' =>'shopinfo.user_id')),					
						),
				    'fields' => array('shoprequest.id','shoprequest.status','shoprequest.user_id','shoprequest.created','shopinfo.shop_name'),
					'conditions' => $conditions,
					'order' => ' shoprequest.created desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 				
        $this->set('s',$s);
        $this->set('result',$result);	
	}		
	
	public function requestview() {	
	    $request_id = intval(Request::get_args('id'));
		$query = array(
				'joins' => array(
					array('type'=>'left','table'=>'shopinfo','conditions'=>array('shoprequest.user_id' =>'shopinfo.user_id')),					
					),
				'fields' => array('shoprequest.*,shopinfo.*'),
				'conditions' => array('shoprequest.id' =>$request_id),
				'group' => ''
				);	
	    $result = POP::exe('[@@:UM shoprequest][@@:QU find:first]',$query);			
		$query = array(
			'fields' => array('area.*'), 
		);
		$return_array = POP::exe('[@@:UM area][@@:QU find:all]',$query);		
	    $resultArea = array();		
	    foreach($return_array as $k=>$v) {
		    $resultArea[$v['area']['id']] = $v['area']['area_name'];
	    }		
        $this->set('resultArea',$resultArea);		
        $this->set('result',$result);
        $this->set('request_id',$request_id);		
	}
	
	public function shopedit() {
	    if (!empty($this->data)) {
		    // 处理post变量
            $shop_id = intval(Request::get_args('shop_id'));
			$query = array(
				'conditions' => array('shopinfo.id' => $shop_id), 
				'fields' => array('shopinfo.*'), 
			);
			$info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);				
			$info = $info['shopinfo'];
			$nowtime = date("Y-m-d H:i:s");
            $post['shop_name'] = Request::get_args('shop_name');
            if($post['shop_name'] != $info['shop_name']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，店铺名被修改",
		        'remind_time' => $nowtime,
	            );							    
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);				
            }
            $post['shop_address'] = Request::get_args('shop_address');
            if($post['shop_address'] != $info['shop_address']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，详细地址被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
            }
            $post['shop_template'] = Request::get_args('shop_template');
            if($post['shop_template'] != $info['shop_template']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，模板被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
            }
            $post['shop_country'] = intval($this->data['shopinfo']['shop_country']);
            if($post['shop_country'] != $info['shop_country']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，国家被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);	
            }
            $post['shop_province'] = intval($this->data['shopinfo']['shop_province']);
            if($post['shop_province'] != $info['shop_province']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，省被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);	
            }
            $post['shop_city'] = intval($this->data['shopinfo']['shop_city']);
            if($post['shop_city'] != $info['shop_city']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，城市被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
            }
            $post['shop_district'] = intval($this->data['shopinfo']['shop_district']);
            if($post['shop_district'] != $info['shop_district']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' =>  "在".$nowtime."，区域被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
            }
            $post['shop_intro'] = Request::get_args('shop_intro');
            if($post['shop_intro'] != $info['shop_intro']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，店铺简介被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
            }
            $post['shop_management'] = Request::get_args('shop_management');
            if($post['shop_management'] != $info['shop_management']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，经营范围被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
            }
            $post['shop_creat_time'] = date("Y-m-d H:i:s");
            $post['shop_categories'] =$this->data['shopinfo']['categories'];
            if($post['shop_categories']==0) {
			    $post['shop_categories'] = $this->data['shopinfo']['categories_parent'];
			}
            if($post['shop_categories'] != $info['shop_categories']){
	            $array = array(
		        'user_id' => $info['user_id'],
		        'remind_info' => "在".$nowtime."，店铺分类被修改",
		        'remind_time' => $nowtime,
	            );
				$safeData['remindinfo'] = $array;								
				POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);				
            }	

			$query['keyid'] = $shop_id;
			$safeData['shopinfo'] = $post;				
			$query['set'] = $safeData;			
			if(POP::exe('[@@:UM shopinfo][@@:QU save]',$query)) {
                $this->flash("修改成功！", Router::url('admin/shop/index'));
            } else {
                $this->flash("修改失败！", Router::url('admin/shop/index'));
            }			
	
		} else {
            $shop_id = intval(Request::get_args('id'));
            $shop_info = array(
	        'shop_name'		=> '',
	        'shop_country'	=> 1,
	        'shop_province'	=> 0,
	        'shop_city'		=> 0,
	        'shop_district'	=> 0,
	        'shop_address'	=> '',
	        'shop_images'	=> '',
	        'shop_template'	=> 'default',
	        'shop_intro'	=> '',
	        'shop_management' => ''
            );
			$query = array(
				'conditions' => array('shopinfo.id' => $shop_id), 
				'fields' => array('shopinfo.*'), 
			);
			$shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);			
		    $shop_info = $shop_info['shopinfo'];
		    $shop_rank = $shop_info['shop_categories'];
			
			$query = array(
				'conditions' => array('shopcategories.parent_id' => 0), 
				'fields' => array('shopcategories.*'), 
			);
			$shop_categories_parent = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);			
            $categories_parent = 0;
            $categories_child = 0;
            if(isset($shop_info['shop_categories'])){
				$query = array(
					'conditions' => array('shopcategories.id' => $shop_info['shop_categories']), 
					'fields' => array('shopcategories.*'), 
				);
				$shop_categories_info = POP::exe('[@@:UM shopcategories][@@:QU find:first]',$query);				
	            if($shop_categories_info['shopcategories']['parent_id'] == 0){
		            $categories_parent = $shop_info['shop_categories'];
	            } else {
		            $categories_parent = $shop_categories_info['shopcategories']['parent_id'];
		            $categories_child = $shop_info['shop_categories'];
	            }
            }
			$query = array(
				'fields' => array('area.*'), 
			);
			$return_array = POP::exe('[@@:UM area][@@:QU find:all]',$query);				
	        $areas_info = array();		
	        foreach($return_array as $k=>$v) {
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['id'] = $v['area']['id'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['parent_id'] = $v['area']['parent_id'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['area_name'] = $v['area']['area_name'];
		    $areas_info[$v['area']['area_type']][$v['area']['id']]['area_type'] = $v['area']['area_type'];
	        }
		    $user_id = $shop_info;
		
		    $this->set('categories_parent', $categories_parent);	
		    $this->set('categories_child', $categories_child);				
		    $this->set('shop_info', $shop_info);				
		    $this->set('areas_info', $areas_info);	
		    $this->set('shop_categories_parent', $shop_categories_parent);
        }		
    }	
	
	public function getshopcategories() {	
        $parent_id = intval($_POST['value']);
		$mode = $_POST['mode'];
        isset($_POST['child_value']) && $child_value = intval($_POST['child_value']);		
        if(!$parent_id || empty($mode)) {
	        exit();
        }
        if(!isset($child_value) ||!$child_value) {
	        $child_value=0;
        }
		$query = array(
			'conditions' => array('shopcategories.parent_id' => $parent_id), 
			'fields' => array('shopcategories.*'), 
		);
		$shop_categories = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);				
        if(!$shop_categories) {
	        exit();
        }	
        $select = '<select name="data['.$mode.'][categories]" >';
        //$select .= '<option value="0">'.$m_langpackage->m_select_province.'</option>';
        foreach($shop_categories as $v) {
	        if($v['shopcategories']['id']==$child_value)
	 	        $select .= '<option value="' . $v['shopcategories']['id'] . '" selected >' . $v['shopcategories']['cat_name'] . '</option>';
	        else
	 	        $select .= '<option value="' . $v['shopcategories']['id'] . '">' . $v['shopcategories']['cat_name'] . '</option>';
        }
        $select .= '</select>';
        echo $select;		
		exit;

	}		
	public function getareas() {
	
        $parent_id = intval($_POST['value']);
        $type = intval($_POST['type']);
        $mode = $_POST['mode'];
        $pre = $_POST['pre'];		
        if(!$parent_id) {
	        exit();
        }
        if($type<0 || $type>2 || empty($mode)) {
	        exit();
        }
		$query = array(
			'conditions' => array('area.parent_id' => $parent_id), 
			'fields' => array('area.*'), 
		);
		$areas_info = POP::exe('[@@:UM area][@@:QU find:all]',$query);		
        header('Content-Type: text/html; charset='.Param::read('App.encoding'));		
        $select = "";
        if($type==0) {
	        $select .= '<select name="data['.$mode.']['.$pre.'province]"   onchange="areachanged(this.value,1);">';
	        $select .= '<option value="0">请选择省份...</option>';
        } elseif($type==1) {
	        $select .= '<select name="data['.$mode.']['.$pre.'city]"   onchange="areachanged(this.value,2);">';
	        $select .= '<option value="0">请选择城市...</option>';
        } elseif($type==2) {
	        $select .= '<select name="data['.$mode.']['.$pre.'district]">';
	        $select .= '<option value="0">请选择区域...</option>';
        }
        foreach($areas_info as $v) {
	        $select .= '<option value="' . $v['area']['id'] . '">' . $v['area']['area_name'] . '</option>';
        } 
        $select .= '</select>';
        echo $select;
		exit;

	}	
	
	public function goodsdownsale() {
        $shop_id=Request::get_args('shop_id');
        if($shop_id){
	        $shop_id=implode(",", $shop_id);
        }else{
	        $shop_id = intval(Request::get_args('id'));
        }	
        if(!$shop_id) {exit('非法操作');}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.id','shopinfo.shop_name'), 
		);
		$shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);			
		$shop_info = $shop_info['shopinfo'];
        $shop_name= $shop_info['shop_name'];		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['goods'] = array('is_on_sale'=>0,'is_best'=>0,'is_new'=>0,'is_hot'=>0,'is_promote'=>0);				
		$query['set'] = $safeData;	
		$query['conditions'] = array('goods.shop_id'=>$shop_id);		
        if(POP::exe('[@@:UM goods][@@:QU save]',$query)) {
	        $remind_id = 2;
	        if (Request::get_args('shop_id')){
		        $shop_id_arr = is_array(Request::get_args('shop_id'))?Request::get_args('shop_id'):array(Request::get_args('shop_id'));
	        }elseif(Request::get_args('id')) {
		        $shop_id_arr = is_array(Request::get_args('id'))?Request::get_args('id'):array(Request::get_args('id'));
	        }
	        foreach ($shop_id_arr as $value){
		        // 获取提醒信息
				$query = array(
					'conditions' => array('remind.id' => $remind_id), 
					'fields' => array('remind.*'), 
				);
				$reminds = POP::exe('[@@:UM remind][@@:QU find:first]',$query);					
				$reminds = $reminds['remind'];				
		        if($reminds['enable']) {
			        $remind_tpl = $reminds['remind_tpl'];
			        // 获取用户的提醒设置
					$query = array(
						'conditions' => array('reminduser.user_id' => $value,'reminduser.remind_id' => $remind_id), 
						'fields' => array('reminduser.*'), 
					);
					$row = POP::exe('[@@:UM reminduser][@@:QU find:first]',$query);						
			        if($row) {
					    $row = $row['reminduser'];
				        $nowtime = date("Y-m-d H:i:s");	
				        $remind_info = remind_info_replace($remind_tpl,array('time'=>$nowtime,'goods'=>$shop_name));
				        if($row['site']) {
					        $array = array(
						    'user_id' => $value,
						    'remind_info' => $remind_info,
						    'remind_time' => $nowtime,
					        );
					        if($reminds['enable']==1){
	                            $safeData['remindinfo'] = $array;								
				                POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);								
					        }
				        }
				        if($row['mail']) {

				        }
				        if($row['im']) {

				        }
				        if($row['mobile']) {

				        }
			        }
		        }
	        }		
			$array = array(
				'log_time' => date("Y-m-d H:i:s"),
				'admin_id' => $this->my->id,
			    'admin_name' => $this->my->username,
				'log_info' => "下架商品"."：$shop_id",
			    'ip_address' => Request::getclientIp(),					
			);
			$safeData['adminlog'] = $array;								
			POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);			
            $this->flash("下架成功！", Router::url('admin/shop/index')); 
        }else {
            $this->flash("下架失败！", Router::url('admin/shop/index')); 
        }		
		
		
	}
	
	public function lockshop() {	
        /* post 数据处理 */
        $value = intval(Request::get_args('v'));
        $shopid=Request::get_args('shop_id');
        if($shopid){
	        $shop_id=implode(",", $shopid);
        }else{
	        $shop_id = intval(Request::get_args('id'));
        }	
        if(!$shop_id) {exit('非法操作');}
		$query = array(
			'conditions' => array('shopinfo.id' => $shop_id), 
			'fields' => array('shopinfo.id','shopinfo.shop_name'), 
		);
		$shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);			
		$shop_info = $shop_info['shopinfo'];
        $shop_name= $shop_info['shop_name'];
        //锁定商铺
		$query  = array();		
		$query['keyid'] = true;
		$safeData['shopinfo'] = array('lock_flg'=>$value);				
		$query['set'] = $safeData;	
		$query['conditions'] = array('shopinfo.id'=>$shop_id);		
		$shop = POP::exe('[@@:UM shopinfo][@@:QU save]',$query);
		
		$setdata = array();
        //商品下架
        if($value){
		    $setdata['is_on_sale'] = 0;
		    $setdata['lock_flg'] = $value;			
        }else{
		    $setdata['is_on_sale'] = 1;
		    $setdata['lock_flg'] = $value;			
        }
		$query  = array();		
		$query['keyid'] = true;
		$safeData['goods'] = $setdata;				
		$query['set'] = $safeData;	
		$query['conditions'] = array('goods.shop_id'=>$shop_id);		
		$goods = POP::exe('[@@:UM goods][@@:QU save]',$query);		
        if($value){
	        if($shop or $goods) {
		        //商铺锁定
		        if ($shop){			
					// 发送提醒
					$remind_id = 5;
					$user_id = $shop_id;
					$shopid = is_array(Request::get_args('id'))?Request::get_args('id'):array(Request::get_args('id'));

					if (Request::get_args('shop_id')){
						$shopid = is_array(Request::get_args('shop_id'))?Request::get_args('shop_id'):array(Request::get_args('shop_id'));
					}elseif(Request::get_args('id')) {
						$shopid = is_array(Request::get_args('id'))?Request::get_args('id'):array(Request::get_args('id'));
					}	
					foreach ($shopid as $k=>$v){					
						// 获取提醒信息
						$query = array(
							'conditions' => array('remind.id' => $remind_id), 
							'fields' => array('remind.*'), 
						);
						$reminds = POP::exe('[@@:UM remind][@@:QU find:first]',$query);							
						$reminds = $reminds['remind'];
						if($reminds['enable']) {
							$remind_tpl = $reminds['remind_tpl'];
							// 获取用户的提醒设置
							$query = array(
								'conditions' => array('reminduser.user_id' => $v,'reminduser.remind_id' => $remind_id), 
								'fields' => array('reminduser.*'), 
							);
							$row = POP::exe('[@@:UM reminduser][@@:QU find:first]',$query);								
							
							if($row) {
								$row = $row['reminduser'];
								$nowtime = date("Y-m-d H:i:s");	
								$remind_info = remind_info_replace($remind_tpl,array('time'=>$nowtime,'goods'=>$shop_name));
								if($row['site']) {
									$array = array(
									'user_id' => $v,
									'remind_info' => $remind_info,
									'remind_time' => $nowtime,
									);
									if($reminds['enable']==1){
										$safeData['remindinfo'] = $array;								
										POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);						
									}
								}
								if($row['mail']) {
									//获得用户信息
									$this->loadModel('user');
									$user_info = $this->user->find('first', array('conditions' => array('user.id' => $v)));
									$user_info = $user_info['user'];
									$user_email = $user_info['email'];
									$user_name = $user_info['username'];
									//邮件发送
									$nowtime = date("Y-m-d H:i:s");	
									$mailbody = "亲爱的用户".$user_name."您好，"."<br />"."您的商品:".$goods_name."于".$nowtime."被管理员锁定了。"."<br />如有疑问请联系管理员，感谢您对本站的支持。<br />( 这是系统自动产生的邮件，请勿回复。)"."<br />";
									$mailtitle = "邮箱提醒";
								   // $this->_lockgoodsConfirmEmail($user_email,$user_name,$mailtitle,$mailbody);
								}
								if($row['im']) {

								}
								if($row['mobile']) {

								}
							}
						}
					}
				    $array = array(
				    'log_time' => date("Y-m-d H:i:s"),
				    'admin_id' => $this->my->id,
					'admin_name' => $this->my->username,
				    'log_info' => "锁定店铺"."：$user_id",
					'ip_address' => Request::getclientIp(),					
			        );
					$safeData['adminlog'] = $array;								
					POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);					
                }

		        if ($goods){
			        // 发送提醒
			        $remind_id = 2;
			        $user_id = $shop_id;
			        // 获取提醒信息
					$query = array(
						'conditions' => array('remind.id' => $remind_id), 
						'fields' => array('remind.*'), 
					);
					$reminds = POP::exe('[@@:UM remind][@@:QU find:first]',$query);						
				    $reminds = $reminds['remind'];	
			        $shopid = is_array(Request::get_args('id'))?Request::get_args('id'):array(Request::get_args('id'));

			        if (Request::get_args('shop_id')){
				        $shopid = is_array(Request::get_args('shop_id'))?Request::get_args('shop_id'):array(Request::get_args('shop_id'));
			        }elseif(Request::get_args('id')) {
				        $shopid = is_array(Request::get_args('id'))?Request::get_args('id'):array(Request::get_args('id'));
			        }					
			        foreach ($shopid as $k=>$v){
				        if($reminds['enable']) {
					        $remind_tpl = $reminds['remind_tpl'];
					        // 获取用户的提醒设置
							$query = array(
								'conditions' => array('reminduser.user_id' => $user_id,'reminduser.remind_id' => $remind_id), 
								'fields' => array('reminduser.*'), 
							);
							$row = POP::exe('[@@:UM reminduser][@@:QU find:first]',$query);						        
							if($row) {
					            $row = $row['reminduser'];
				                $nowtime = date("Y-m-d H:i:s");	
						        $remind_info = remind_info_replace($remind_tpl,array('time'=>$nowtime,'goods'=>$shop_name));
						        if($row['site']) {
							        $array = array(
								    'user_id' => $v,
								    'remind_info' => $remind_info,
								    'remind_time' => $nowtime,
							        );
							        if($reminds['enable']==1){
										$safeData['remindinfo'] = $array;								
										POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
							        }
						        }
						        if($row['mail']) {
						            //获得用户信息
							        $this->loadModel('user');
							        $user_info = $this->user->find('first', array('conditions' => array('user.id' => $v)));
						            $user_info = $user_info['user'];
						            $user_email = $user_info['email'];
						            $user_name = $user_info['username'];
						            //邮件发送
						            $nowtime = date("Y-m-d H:i:s");	
						            $mailbody = "亲爱的用户".$user_name."您好，"."<br />"."您的商品:".$goods_name."于".$nowtime."被管理员锁定了。"."<br />如有疑问请联系管理员，感谢您对本站的支持。<br />( 这是系统自动产生的邮件，请勿回复。)"."<br />";
						            $mailtitle = "邮箱提醒";
	                                // $this->_lockgoodsConfirmEmail($user_email,$user_name,$mailtitle,$mailbody);								
						        }
						        if($row['im']) {

						        }
						        if($row['mobile']) {

						        }
					        }
				        }
			        }
		        }				
                $this->flash("商品锁定成功", Router::url('admin/shop/index'));					
	        } else {
		        $this->flash("商品锁定失败", Router::url('admin/shop/index'));	
	        }
        }else{
	        if($shop or $goods) {
		        //商铺解除锁定
		        // 发送提醒
		        $remind_id = 6;
		        $user_id = $shop_id;
		        // 获取提醒信息
				$query = array(
					'conditions' => array('remind.id' => $remind_id), 
					'fields' => array('remind.*'), 
				);
				$reminds = POP::exe('[@@:UM remind][@@:QU find:first]',$query);					
				$reminds = $reminds['remind'];	
			    if (Request::get_args('shop_id')){
				    $shopid = is_array(Request::get_args('shop_id'))?Request::get_args('shop_id'):array(Request::get_args('shop_id'));
			    }elseif(Request::get_args('id')) {
				    $shopid = is_array(Request::get_args('id'))?Request::get_args('id'):array(Request::get_args('id'));
			    }				
		        foreach ($shopid as $v){
			        if($reminds['enable']) {
				        $remind_tpl = $reminds['remind_tpl'];
				        // 获取用户的提醒设置
						$query = array(
							'conditions' => array('reminduser.user_id' => $user_id,'reminduser.remind_id' => $remind_id), 
							'fields' => array('reminduser.*'), 
						);
						$row = POP::exe('[@@:UM reminduser][@@:QU find:first]',$query);							 
				        if($row) {
					        $row = $row['reminduser'];
				            $nowtime = date("Y-m-d H:i:s");	
					        $remind_info = remind_info_replace($remind_tpl,array('time'=>$nowtime,'goods'=>$shop_name));
					        if($row['site']) {
						        $array = array(
							    'user_id' => $v,
							    'remind_info' => $remind_info,
							    'remind_time' => $nowtime,
						        );
						        if($reminds['enable']==1){
									$safeData['remindinfo'] = $array;								
									POP::exe('[@@:UM remindinfo][@@:QU save]',$safeData);
						        }
					        }
					        if($row['mail']) {

					        }
					        if($row['im']) {

					        }
					        if($row['mobile']) {

					        }
				        }
			        }
		        }
				$array = array(
				    'log_time' => date("Y-m-d H:i:s"),
				    'admin_id' => $this->my->id,
					'admin_name' => $this->my->username,
				    'log_info' => "锁定店铺"."：$user_id",
					'ip_address' => Request::getclientIp(),					
			    );
	            $safeData['adminlog'] = $array;									
				POP::exe('[@@:UM adminlog][@@:QU save]',$safeData);	
		        $this->flash("店铺锁定成功", Router::url('admin/shop/index'));	
	        } else {
		        $this->flash("店铺锁定失败", Router::url('admin/shop/index'));	
	        }		
        }			
		
	}		
	
	function _lockgoodsConfirmEmail($addr, $username, $mailtitle ,$mailbody) {
		$this->Email->to = $addr;
		$this->Email->bcc = array('noreply@ggl.com');  
		$this->Email->subject = $mailtitle;
		$this->Email->replyTo = 'noreply@ggl.com';
		$this->Email->from = '"GGL" <noreply@ggl.com>';
		$this->Email->template = 'confirm_getpasswd'; // note no '.ctp'
		//Send as 'html', 'text' or 'both' (default is 'text')
		$this->Email->sendAs = 'both'; // because we like to send pretty mail
		//Set view variables as normal
		$this->set('user', $username);
		$this->set('ConfirmationCode', $mailbody);
		
		//------ 采用SMTP方式发送邮件
		//SMTP Options
		$this->Email->smtpOptions = array(
			'port'=>'465', 
			'timeout'=>'30',
			'host' => 'ssl://smtp.gmail.com',
			'username'=>'noreply@ggl.com',
			'password'=>'zxcvl#8v',
		);

		//Set delivery method 
		//使用SMTP方式需要确认的是：/cake/libs/controller/components/email.php 中的 HELO 修改为 EHLO (1.3版本不再需要修改)
		//email.php更改成var $xMailer = 'GGL Email';
		$this->Email->delivery = 'smtp';
		//------ SMTP设置完成，如要使用PHP函数发送邮件，则直接注释该部分		
		//Do not pass any args to send()
		$this->Email->send();	
	}	
	

	
	public function categorieslist() {
		$query = array(
			'order' => ' sort_order asc,id asc ', 
			'fields' => array('shopcategories.*'), 
		);
		$result_category = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);	
		$category_dg = $this->get_dg_category($result_category);
        $this->set('category_dg',$category_dg);			
	}	
	public function categoriesadd() {
        $this->loadModel('shopcategories');		
		if (!empty($this->data)) {	
			$query = $this->data;					
			if (POP::exe('[@@:UM shopcategories][@@:QU save]',$query)) {
				$this->flash('更新成功！', Router::url('admin/shop/categorieslist'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/shop/categorieslist'));
			}				
	    }else {
			$query = array(
				'order' => ' sort_order asc,id asc ', 
				'fields' => array('shopcategories.*'), 
			);
			$result_category = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);		
			$category_dg = $this->get_dg_category_select($result_category);
			$this->set('category_dg',$category_dg);	
		}
		
	}
	public function categoriesedit($id) {	
		if (!empty($this->data)) {
			$query['keyid'] = $id;
			$safeData['shopcategories'] = $this->data;				
			$query['set'] = $safeData;			
			if(POP::exe('[@@:UM shopcategories][@@:QU save]',$query)) {		
				$this->flash('更新成功！', Router::url('admin/shop/categorieslist'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/shop/categorieslist'));
			}			 	
	    }else {
			$query = array(
				'order' => ' sort_order asc,id asc ', 
				'fields' => array('shopcategories.*'), 
			);
			$result_category = POP::exe('[@@:UM shopcategories][@@:QU find:all]',$query);			
			$category_dg = $this->get_dg_category_select($result_category);
			foreach($result_category as $v) {
				if($v['shopcategories']['id']==$id) {
					$info = $v['shopcategories'];
				}
			}
			$this->set('info',$info);	
			$this->set('category_dg',$category_dg);	
		}
		
	}
	
	public function categoriesdel($id) {
		$query = array(
			'conditions' => array('shopcategories.parent_id' => $id), 
			'fields' => array('shopcategories.*'), 
		);
		$parent_id_last = POP::exe('[@@:UM shopcategories][@@:QU find:first]',$query);		
        if(isset($parent_id_last['shopcategories']['id'])) {
		    $this->flash('删除失败，请先删除该项的子项目！', Router::url('admin/shop/categorieslist/'));			 
		}else {
			$query = array('keyid'=>$id);			
			POP::exe('[@@:UM shopcategories][@@:QU del]',$query);			
		    $this->flash('删除成功！', Router::url('admin/shop/categorieslist/'));
        }
	}
	
	public function getnbsp($i) {
	    $str = '';
	    if($i) {
		    for($j=0; $j<$i; $j++) {
			    $str .= "　";
		    }
	    }
	    return $str;
    }

	public function get_dg_category($array,$parentid=0,$level=0,$add=2) {
	$str_pad = $this->getnbsp($level);
	$newarray = array();
	$temp = array();
	foreach($array as $v) {
		if($v['shopcategories']['parent_id'] == $parentid) {
			$newarray[] = array(
				'id' => $v['shopcategories']['id'],
				'cat_name' => $v['shopcategories']['cat_name'],
				'parent_id' => $v['shopcategories']['parent_id'],
				'shops_num' => $v['shopcategories']['shops_num'],
				'sort_order' => $v['shopcategories']['sort_order'],
				'str_pad' => $str_pad
			);
			$temp = $this->get_dg_category($array,$v['shopcategories']['id'],($level+$add));
			if($temp) {
				$newarray = array_merge($newarray, $temp);
			}
		}
	}
	return $newarray;
    }	
	public function get_dg_category_select($array,$parentid=0,$level=0,$add=2) {
	$str_pad = $this->getnbsp($level);
	$newarray = array();
	$temp = array();
	foreach($array as $v) {
		if($v['shopcategories']['parent_id'] == $parentid) {
			$newarray[] = array(
				'id' => $v['shopcategories']['id'],
				'cat_name' => $v['shopcategories']['cat_name'],
				'parent_id' => $v['shopcategories']['parent_id'],
				'shops_num' => $v['shopcategories']['shops_num'],
				'sort_order' => $v['shopcategories']['sort_order'],
				'str_pad' => $str_pad
			);
			//$temp = get_dg_category($array,$v['cat_id'],($level+$add));
			if($temp) {
				$newarray = array_merge($newarray, $temp);
			}
		}
	}
	return $newarray;
    }

	
}

function remind_info_replace($tpl,$array) {
	$arr1 = $arr2 = array();
	foreach($array as $k=>$v) {
		$arr1[] = '{'.$k.'}';
		$arr2[] = $v;
	}
	return str_replace($arr1,$arr2,$tpl);
}
?>